import '../entity/book_entity.dart';
import '../main.dart';

void bookInsert(bookNo, BookEntity data) {
  books.put(bookNo, data);
}


BookEntity? bookQuery(String bookNo) {
  if (books.get(bookNo) != null) {
    return books.get(bookNo);
  } else {
    return null;
  }
}

// List<BookEntity> bookQueryByDoorNo(String doorNo) {
//   var res = books.values.toList();
//   List<BookEntity> cc = res.cast<BookEntity>();
//   return cc.where((element) => element.doorNo == doorNo).toList();
// }

List<BookEntity> bookQueryByKey(String searchKey,{int? limit}) {
  List<BookEntity> list = [];
  list.addAll(books.values.cast<BookEntity>().where((element) => element.toString().contains(searchKey)));
  if(limit !=null && list.length > limit){
    return list.take(limit).toList();
  }
  return list.cast<BookEntity>();
}

List<BookEntity> bookQueryAll() {
  var res = books.values.toList();
  List<BookEntity> cc = res.cast<BookEntity>();
  return cc;
}

void bookDelete(bookNo) {
  books.delete(bookNo);
}
void bookClear() {
  books.deleteAll(books.keys);
}